<style >
     .layui-table-cell{
         height: auto!important;
         white-space: normal;
     }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini">
            <div class="layui-form toolbar" id="tbToolBar">
                <div class="layui-form-item">
					<div class="layui-inline">
						<button id="btnAddBizModel" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>在线模型</button>
						<button id="btnUploadBizModel" class="layui-btn icon-btn"><i class="layui-icon">&#xe681;</i>本地模型</button>
						<span style="color:red;">上传模型格式必须zip,具体zip包目录结构请咨询管理员或查阅<a id="btnUploadBizModelForDoc" href="javascript:void(0);" style="color: blue;">文档</a>！！！</span>
					</div>
                    <div class="layui-inline" style="float: right;margin-right: 0px;">
						<div class="layui-inline" style="padding-right: 0px;margin-right: 0px;">
							<label class="layui-form-label w-auto">分类</label>
							<div class="layui-input-inline mr0">
								<input id="parentId" type="text" name="parentId"   lay-filter="parentId" autocomplete="off" placeholder="请选择" value="" class="layui-input">
							</div>
						</div>                    
						<div class="layui-inline" style="padding-right: 0px;margin-right: 0px;">
							<label class="layui-form-label w-auto">名称</label>
							<div class="layui-input-inline mr0">
								<input name="name" class="layui-input" type="text" placeholder="请输入名称"/>
							</div>
						</div>
						<div class="layui-inline" style="padding-right: 0px;margin-right: 0px;">
							<button class="layui-btn icon-btn" lay-filter="formSubSearchBizModel" lay-submit>
								<i class="layui-icon">&#xe615;</i>查询
							</button>
						</div>					
					</div>
				</div>
				<table class="layui-table" id="tableBizModel" lay-filter="tableBizModel"></table>
			</div>
		</div>
	</div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarBizModel">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="tableStateBizModel">
#[[  {{#  if(d.enabled === '1'){ }}
    <span style="color: #23aa2e;">正常</span>
  {{#  } else { }}
	<span style="color: #ff0000;">禁止</span>
  {{#  } }}
]]#
</script>


<!-- 表单弹窗-->
<script type="text/html" id="modelBizModel"> 
    <form id="modelBizModelForm" lay-filter="modelBizModelForm" class="layui-form model-form" style="padding: 5px;">
        <input name="id" type="hidden"  />
		<table class="formTable">
			<tr>
				<td rowspan="4" style="text-align: center;">
			    	<img id="BizModel-cover" src="#(ctx)/assets/img/thum-default.jpg" width="135" height="135">
				</td>
			</tr>
			<tr>
				<td>分类</td>
				<td colspan="2" style="text-align: left;">
                	<input id="classifyId" lay-filter="classifyId" name="classifyId"  placeholder="请选择" type="text" class="layui-input" lay-verType="tips" lay-verify="required" required/>
                </td>
			</tr>
			<tr>
				<td>名称</td>
				<td colspan="2" style="text-align: left;">
                	<input name="name"  placeholder="请输入名称" type="text" class="layui-input" lay-verType="tips" lay-verify="required" required/>
                </td>
			</tr>
			<tr>
				<td>标识</td>
				<td colspan="2" style="text-align: left;">
                	<input name="code"  placeholder="请输入标识" type="text" class="layui-input" />
                </td>
			</tr>			
			<tr>
				<td>格式</td>
				<td colspan="2" style="text-align: left;">
			      <select name="type" lay-filter="type"  lay-verify="required" required>
			        <option value="">--请选择--</option>
			        <option value="3DTILES">3dtiles</option>
			        <option value="GLTF">gltf</option>
					<option value="OBJ">obj(仅用于视频融合)</option>
			      </select>
                </td>
			</tr> 
			<tr>
				<td>地址</td>
				<td colspan="3">
                	<input name="url"  placeholder="请输入" type="text" class="layui-input" lay-verType="tips" lay-verify="required" required/>
                </td>              
			</tr>
			<tr>
				<td>相对路径</td>
				<td colspan="3" style="text-align: left;">
			      <select name="isRelUrl" lay-filter="isRelUrl"  lay-verify="required" required>
			        <option value="">--请选择--</option>
			        <option value="0" >否</option>
			        <option value="1" >是</option>
			      </select>
                </td>
			</tr>
			<tr>
				<td>来源</td>
				<td colspan="3" style="text-align: left;">
			      <select name="source" lay-filter="source"  lay-verify="required" required>
			        <option value="">--请选择--</option>
			        <option value="0" >本地库</option>
			        <option value="1" >在线库</option>
			      </select>
                </td>
			</tr>
			<tr>
				<td>是否可用</td>
				<td colspan="3" style="text-align: left;">
			      <select name="enabled" lay-filter="status"  lay-verify="required" required>
			        <option value="">--请选择--</option>
			        <option value="0" >禁用</option>
			        <option value="1" >启用</option>
			      </select>
                </td>
			</tr>
			<tr>
				<td colspan="4">
		            <button class="layui-btn" lay-filter="modelSubmitBizModelForm" lay-submit>保存</button>
                </td>
			</tr>												
		</table>    
    </form>
</script>

<script type="text/html" id="modelBizModelForDoc"> 
<div style="padding: 20px; background-color: #F2F2F2;">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md6">
      <div class="layui-card">
        <div class="layui-card-header">人工模型</div>
        <div class="layui-card-body">
         	人工模型只支持gltf格式，其它格式请自行转换！<br>
          	zip格式目录：根目录打包<br>
			备注：模型过大，建议转换成tileset格式！
        </div>
      </div>
    </div>
    <div class="layui-col-md6">
      <div class="layui-card">
        <div class="layui-card-header">3D Tiles</div>
        <div class="layui-card-body">
          	三维倾斜模型、人工建模、BIM模型等等，都可以转换成3D Tile<br>
          	zip格式目录：根目录打包，tileset.json位于根目录！<br>
        </div>
      </div>
    </div>
  </div>
</div> 
</script>

<!-- js部分 -->
<script>
layui.extend({
    treeSelect: '/treeSelect/treeSelect'
}).use(['layer', 'form', 'table', 'util','upload','treeSelect', 'admin', 'config','colorpicker'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var upload = layui.upload;
        var util = layui.util;
        var admin = layui.admin;
        var config = layui.config;
        var treeSelect = layui.treeSelect;
        
        var colorpicker = layui.colorpicker;
        // 渲染表格
        var insTb = table.render({
            elem: '#tableBizModel',
            url: '#(ctx)/biz/model/listData', 
            page: true,
            toolbar: false,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号'},
                {
                    align: 'center', templet: function (d) {
                    	if(d.img)
                        	return '<img src="#(ctx)' + d.img + '" width="60px"  />';
                        else
                        	return '';
                    }, title: '缩略图', width: 90, unresize: true
                },
                {field: 'name', sort: false, title: '名称'},
                {field: 'code', sort: false, title: '标识'},
                {field: 'type', sort: false, title: '格式'},
                {
                    field: 'source', sort: false, templet: function (d) {
                    	if(d.source=='0'){
                    		return "本地库"
                    	}
                    	else if(d.source=='1'){
                    		return "在线库"
                    	}else{
                    		return ""
                    	}
                    }, title: '来源'
                },                
                {
                    field: 'createTime', sort: false, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
				{field: 'enabled', sort: false, templet: '#tableStateBizModel', title: '状态', align: 'center'},
                {align: 'left', toolbar: '#tableBarBizModel', title: '操作'}
            ]],
        });

        // 添加
		$('#btnAddBizModel').click(function () {
			 showEditModel();
		});
		$('#btnUploadBizModelForDoc').click(function () {
			showUploadDocModel();
		});
		

        // 搜索
        form.on('submit(formSubSearchBizModel)', function (data) {
            insTb.reload({where: data.field}, 'data');
        });

        // 工具条点击事件
        table.on('tool(tableBizModel)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
            	showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(data.id, data.name);
            }
        });
        
	    treeSelect.render({
	        elem: '#parentId',
	        data: '#(ctx)/biz/model/classify/selectTree',
	        type: 'get',
	        placeholder: '请选择',
	        search: true,
	        click: function(d){
	        },
	        success: function (d) {
	        }
	    });

        // 删除
        function doDel(id, name) {
            layer.confirm('确定要删除“【<span style="color:red;">' + name + '</span>】”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('biz/model/delete', {
                    id: id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.message, {icon: 1});
                        insTb.reload({}, 'data');
                    } else {
                        layer.msg(res.message, {icon: 2});
                    }
                }, 'post');
            });
        }
        
        upload.render({
	          elem: '#btnUploadBizModel'
	          ,url: '#(ctx)/biz/model/upload'
	          ,accept: 'file'
	          ,exts: 'zip'
	          ,before: function(obj){ 
	      	    layer.load(); 
	      	  }
	      	  ,done: function(res, index, upload){
	      		layer.closeAll('loading'); //关闭loading
	              if (200==res.code) {
	                  layer.msg(res.message, {icon: 1});
	                  insTb.reload();
	              } else {
	                  layer.msg(res.message, {icon: 2});
	              }
	      	  }
	      	  ,error: function(index, upload){
	      	    layer.closeAll('loading');
	      	    layer.msg("上传失败！", {icon: 2});
	      	  }       
        });


        // 显示表单弹窗
        function showEditModel(data) {
            layer.open({
                type: 1,
                shade: 0.3,
                offset: 't'	,
                title: (data ? '修改' : '添加') + '',
                area:'500px',
                resize :false,
                content: $('#modelBizModel').html(),
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    form.val('modelBizModelForm', data);  // 回显数据
                    
                    if(data && data.img){
                    	$('#BizModel-cover').attr("src","#(ctx)"+data.img);
                    }
                  
					if(data){
				        upload.render({
				            elem: '#BizModel-cover'
				            ,url: '#(ctx)/biz/model/uploadThumbnail?id='+data.id
				            ,accept: 'images'
				         	,before: function(obj){ 
				      	    layer.load(); 
				      	  }
				      	  ,done: function(res, index, upload){
				      		layer.closeAll('loading'); //关闭loading
				              if (200==res.code) {
				                  layer.msg(res.message, {icon: 1});
				                  insTb.reload();
				                  $('#BizModel-cover').attr("src","#(ctx)/"+res.data.img);
				              } else {
				                  layer.msg(res.message, {icon: 2});
				              }
				      	  }
				      	  ,error: function(index, upload){
				      	    	layer.closeAll('loading');
				      	   	 	layer.msg("上传失败！", {icon: 2});
				      	  }       
				        });
					}

                    
                    
            	    treeSelect.render({
            	        elem: '#classifyId',
            	        data: '#(ctx)/biz/model/classify/selectTree',
            	        type: 'get',
            	        placeholder: '请选择',
            	        search: true,
            	        click: function(d){
            	        },
            	        success: function (d) {
            	        	if(data && data.classifyId)
                        	treeSelect.checkNode('classifyId', data.classifyId);
            	        }
            	    });
                    
    			    form.on('submit(modelSubmitBizModelForm)', function (data) {
    			        layer.load(2);
    			        var formData={}; 
    			        for(var key in data.field){
    			        	if('file'!==key)
    			        		formData['bizModel.'+key]=data.field[key];
    			        }
                        admin.req("biz/model/save",formData, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.message, {icon: 1});
                                insTb.reload({}, 'data');
                            } else {
                                layer.msg(res.message, {icon: 2});
                            }
                        }, 'post');
    			        return false;
    			    });
    			    form.render(); 
                },
                end:function(){
                }
            });
        }
        
        function showUploadDocModel() {
            layer.open({
                type: 1,
                shade: 0.3,
                offset: 't'	,
                title: '上传模型格式说明',
                area:'500px',
                resize :false,
                content: $('#modelBizModelForDoc').html(),
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                },
                end:function(){
                }
            });
        }        

    });
</script>
